#include "chessdetectionwindow.h"
#include <QApplication>
#include <QDebug>
#include <QCloseEvent>

ChessDetectionWindow::ChessDetectionWindow(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle("棋盘检测");
    resize(800, 600);
    
    chessDetector = new ChessDetector(this);
    
    QVBoxLayout *layout = new QVBoxLayout(this);
    
    // 图像显示区域
    imageLabel = new QLabel(this);
    imageLabel->setAlignment(Qt::AlignCenter);
    imageLabel->setMinimumSize(640, 480);
    imageLabel->setStyleSheet("background-color: black;");
    layout->addWidget(imageLabel);
    
    // 信息显示区域
    infoText = new QTextEdit(this);
    infoText->setReadOnly(true);
    layout->addWidget(infoText);
    
    // 控制按钮
    startButton = new QPushButton("开始检测", this);
    layout->addWidget(startButton);
    
    // 连接信号和槽
    connect(chessDetector, &ChessDetector::imageProcessed, 
            this, &ChessDetectionWindow::updateImage);
    connect(chessDetector, &ChessDetector::detectionInfo,
            this, &ChessDetectionWindow::updateDetectionInfo);
    connect(startButton, &QPushButton::clicked,
            this, &ChessDetectionWindow::startDetection);
}

void ChessDetectionWindow::startDetection()
{
    if (chessDetector) {
        chessDetector->startDetection();
        startButton->setEnabled(false);
    }
}

void ChessDetectionWindow::updateImage(const QImage &image)
{
    imageLabel->setPixmap(QPixmap::fromImage(image).scaled(
        imageLabel->width(), imageLabel->height(), 
        Qt::KeepAspectRatio, Qt::SmoothTransformation
    ));
}

void ChessDetectionWindow::updateDetectionInfo(const QString &info)
{
    infoText->setText(info);
}
